package com.intel.two.service.smarthome.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intel.two.common.R;
import com.intel.two.mapper.smarthome.ServicemanMapper;
import com.intel.two.pojo.smarthome.Guarantee;
import com.intel.two.pojo.smarthome.Household;
import com.intel.two.pojo.smarthome.Serviceman;
import com.intel.two.service.smarthome.ServicemanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @BelongsProject: intelligentcommunity
 * @BelongsPackage: com.intel.two.service.smarthome.impl
 * @Author: yanhongwei
 * @CreateTime: 2022-10-15  14:43
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class ServicemanServiceImpl implements ServicemanService {

    @Autowired
    private ServicemanMapper servicemanMapper;

    @Override
    public R servicemanList(Map<String, Object> parameters) {
        Integer limit = Integer.parseInt((String) parameters.get("limit"));
        Integer page = Integer.parseInt((String) parameters.get("page"));
//        开启分页
        PageHelper.startPage(page, limit);
        List<Serviceman> servicemenList =  servicemanMapper.servicemanList(parameters);
        Map<String, Object> map = new HashMap<>();
//        获取分页的相关信息
        PageInfo<Serviceman> pageInfo = new PageInfo(servicemenList);
        map.put("servicemenList", servicemenList);//学生信息
        map.put("count", pageInfo.getTotal());//总记录数
        return R.ok().data(map);
    }

    @Override
    public R servicemanAdd(Serviceman serviceman) {

        int i = servicemanMapper.servicemanAdd(serviceman);
        return i>0?R.ok():R.error();
    }

    @Override
    public R servicemanDel( List<Serviceman> servicemans) {

        List<Integer> collect = servicemans.stream().map(serviceman -> serviceman.getServiceman_id()).collect(Collectors.toList());

        int i = servicemanMapper.servicemanDel(collect);

        return i>0?R.ok():R.error();
    }

    @Override
    public R servicemanList1() {
       List<Serviceman> servicemens =  servicemanMapper.servicemanList1();

        return R.ok().data("servicemens",servicemens);
    }
}
